package com.zhss.designpattern.prototype;

/**
 * @author fanwei
 * @desc 原型模式(复制模式) 就是在要拷贝的类里实现一个clone()方法，自己拷贝自己
 *         拷贝的时候，就两个概念，浅拷贝，深拷贝（原型模式就是这个）
 *         很多地方要克隆这个对象，不要自己维护克隆的逻辑，即使克隆逻辑修改了，只要在clone()方法里面修改
 */
public class PrototypePatternDemo {

    public static class Component{
        private String name;

        public Component(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return new Component(getName());
        }
    }

    public static class Product{
        private String name;
        private Component component;

        public Product(String name, Component component) {
            this.name = name;
            this.component = component;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Component getComponent() {
            return component;
        }

        public void setComponent(Component component) {
            this.component = component;
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
            // 深考别，递归对自己引用的对象也进行拷贝
            return new Product(getName(),(Component) getComponent().clone());
        }
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        Product product = new Product("测试产品", new Component("测试组件"));
        Product copyProduct = (Product) product.clone();
        System.out.println(copyProduct == product);
    }
}
